<?php
namespace App\Http\Controllers\Business;

use App\Models\WaterMeter;
use App\Models\WaterMeterRadix;
use App\Models\WaterMeterDayConsumption;
use Illuminate\Support\Facades\DB;
use Log;

class ChartModule {
    //首页
    public static function index() {
        $firstLevelNode = "ch00"; //TODO
//        $sql = "select A.number,A.radix,A.updated_at, min(A.radix- B.radix) as dif from watermeter_radix A  inner join watermeter_radix B ".
//            "on (A.id>B.id and A.number=B.number and A.number='".$firstLevelNode."') group by A.number, A.id order by A.id desc, updated_at desc;";
//        $lists = DB::select($sql);
        $lists = WaterMeterDayConsumption::where('number', $firstLevelNode)->orderBy('current_date', 'asc')->select('current_date', 'consumption')->get();
        $data = [];
        foreach($lists as $list) {
            $dif = $list->consumption;
            $testDate = $list->current_date;
            $childList = [
                'my_date' => date('Y-m-d', strtotime($testDate)),
                'year' => date('Y', strtotime($testDate)),
                'month' => date('m', strtotime($testDate)),
                'day' => date('d', strtotime($testDate)),
                'dif' => $dif
            ];
            array_push($data, $childList);
        }
        return ['status'=>'success', 'data'=>$data];
    }
}

?>
